package com.example.campuslink.commen.adpter


import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.core.CoreAdapter
import com.example.campuslink.commen.core.CoreViewHolder
import com.example.campuslink.databinding.AdapterSchoolUserBinding
import com.example.campuslink.viewModel.MessageViewModel

class SchoolUserListAdapter(vm: MessageViewModel) :
    CoreAdapter<AdapterSchoolUserBinding, MessageViewModel, String>(
        BR.data,
        BR.viewModel,
        vm
    ) {

    override fun getItemLayoutId(viewType: Int): Int {
        mViewModel?.getAttentionListAndFansList()
        return R.layout.adapter_school_user
    }


    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val userNumber = mListData[position]

        holder.binding.apply {
            // 设置默认值
            root.findViewById<TextView>(R.id.tv_userName).text = "加载中..."
            Glide.with(holder.itemView.context).load(R.drawable.ic_empty)
                .apply(RequestOptions.circleCropTransform())
                .into(root.findViewById<ImageView>(R.id.iv_userImg))
            if (mViewModel?.attentionList?.value?.contains(userNumber) ?: false) {
                root.findViewById<Button>(R.id.btn_attention).text = "已关注"
                if (mViewModel?.fansList?.value?.contains(userNumber)!!) {
                    root.findViewById<Button>(R.id.btn_attention).text = "朋友"
                }
                root.findViewById<Button>(R.id.btn_attention).setOnClickListener({
                    mViewModel?.removeAttentionAndFans(userNumber)
                    root.findViewById<Button>(R.id.btn_attention).text = "加载中..."
                })
            } else {
                root.findViewById<Button>(R.id.btn_attention).text = "关注"
                root.findViewById<Button>(R.id.btn_attention).setOnClickListener({
                    mViewModel?.addAttentionAndFans(userNumber)
                    root.findViewById<Button>(R.id.btn_attention).text = "加载中..."
                })
            }

            root.setOnClickListener({
                mViewModel?.jumpToUserMainActivity(userNumber)
            })

            // 获取用户信息
            mViewModel?.getUserInfo(userNumber)

            // 观察数据变化 - 使用 viewLifecycleOwner 或 holder.itemView 的生命周期
            mViewModel?.userInfoMap?.observe(holder.itemView.context as LifecycleOwner) { map ->
                map[userNumber]?.let { info ->
                    root.findViewById<TextView>(R.id.tv_userName).text = info.userName
                    Glide.with(holder.itemView.context).load(info.userAvatar)
                        .error(R.drawable.ic_empty)
                        .into(root.findViewById<ImageView>(R.id.iv_userImg))
                }
            }
            if (position == mListData.size - 1) {
                mViewModel?.getUserInfo(userNumber)
            }
        }


    }
}